﻿using System;
using System.ComponentModel;
using System.Reflection;

namespace SLS.ExClassLib.MultiThreading.Extensions
{
    public static class EnumExtensions
    {
        public static string ConvertToString(this Enum value)
        {
            if (value == null)
                throw new ArgumentNullException("value");
            Type type = value.GetType();
            FieldInfo fieldInfo = type.GetField(Enum.GetName(type, value));
            var descriptionAttribute =
                (DescriptionAttribute) Attribute.GetCustomAttribute(
                                           fieldInfo, typeof (DescriptionAttribute));

            if (descriptionAttribute != null)
                return descriptionAttribute.Description;
            return value.ToString();
        }
    }
}